package com.x.compoent.gnss;

import java.util.concurrent.CopyOnWriteArrayList;

import com.x.component.connectivity.FrameworkServicesModule;
import android.content.Context;
import android.location.GnssStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class GnssCompoent {

	private static GnssCompoent mInstance = null;

	private static final Object mLock = new Object();

	private Context mContext;

	private LocationManager mLocationManager;

	private GnssCompoent() {

	}

	public static GnssCompoent getInstance() {
		if (null == mInstance) {
			synchronized (mLock) {
				if (null == mInstance) {
					mInstance = new GnssCompoent();
				}
			}
		}
		return mInstance;
	}

	public void init(Context context) {
		this.mContext = context;
		if (this.mContext != null) {
			mLocationManager = FrameworkServicesModule.provideLocationManager(mContext);
			mLocationManager.registerGnssStatusCallback(gnssStatusCallback);
			startGnssListening();
		}
	}

	public void startGnssListening() {
		if (mLocationManager != null && this.mContext != null) {
			mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
		}
	}

	private final LocationListener locationListener = new LocationListener() {
		@Override
		public void onLocationChanged(Location location) {
			if (location != null) {
				float speed = location.getSpeed(); // 速度，单位：m/s
				float speedKmh = speed * 3.6f; // 转换为km/h
				Log.d("GnssCompoent", "Speed: " + speedKmh + " km/h");
			}
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			for (GnssTool tool : mGnssToolList) {
				tool.onStatusChanged(provider, status, extras);
			}
		}

		@Override
		public void onProviderEnabled(String provider) {
			for (GnssTool tool : mGnssToolList) {
				tool.onProviderEnabled(provider);
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			for (GnssTool tool : mGnssToolList) {
				tool.onProviderDisabled(provider);
			}
		}
	};

	private int calculateSignalLevel(float snr) {
		// Example calculation, adjust the thresholds according to your needs
		if (snr >= 35) {
			return 4; // Excellent
		} else if (snr >= 30) {
			return 3; // Good
		} else if (snr >= 25) {
			return 2; // Fair
		} else if (snr >= 20) {
			return 1; // Poor
		} else {
			return 0; // No signal
		}
	}

	private final GnssStatus.Callback gnssStatusCallback = new GnssStatus.Callback() {
		@Override
		public void onSatelliteStatusChanged(GnssStatus status) {
			super.onSatelliteStatusChanged(status);
			Log.d("GnssCompoent", "Gnss onSatelliteStatusChanged " + status.toString());

			int satelliteCount = status.getSatelliteCount();
			float totalSnr = 0;
			int validSatelliteCount = 0;

			for (int i = 0; i < satelliteCount; i++) {
				float snr = status.getCn0DbHz(i);
				if (snr > 0) {
					totalSnr += snr;
					validSatelliteCount++;
				}
			}

			float averageSnr = validSatelliteCount > 0 ? totalSnr / validSatelliteCount : 0;
			int signalLevel = calculateSignalLevel(averageSnr);
			Log.d("GnssCompoent", "Gnss signalLevel " + signalLevel);
			for (GnssTool tool : mGnssToolList) {
				tool.onGnssSignalStrengthChanged(signalLevel);
			}
		};
	};

	private CopyOnWriteArrayList<GnssTool> mGnssToolList = new CopyOnWriteArrayList<GnssTool>();

	public boolean setGnssTool(GnssTool GnssTool) {
		if (GnssTool != null && !mGnssToolList.contains(GnssTool)) {
			mGnssToolList.add(GnssTool);
			return true;
		}

		return false;
	}

	public boolean removeGnssTool(GnssTool GnssTool) {
		if (GnssTool != null && mGnssToolList.contains(GnssTool)) {
			mGnssToolList.remove(GnssTool);
			return true;
		}
		return false;
	}

}
